package cn.jinjimi.app.main.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.MediaController;
import android.widget.VideoView;

import cn.jinjimi.base.util.ToastUtils;

/**
 * Created by Roy
 * Date: 15/12/2
 */
public class MyVideoView extends VideoView implements MediaController.MediaPlayerControl {

    private boolean netAvaliable = true;
    private boolean forbiddenStart; // 断网情况下禁止播放

    public MyVideoView(Context context) {
        super(context);
    }

    public MyVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setNetAvaliable(boolean canSeek) {
        this.netAvaliable = canSeek;
    }

    public void setForbiddenStart(boolean canStart) {
        this.forbiddenStart = canStart;
    }

    @Override
    public void seekTo(int msec) {
        if (netAvaliable) {
            super.seekTo(msec);
        } else {
            if (msec < getBufferPercentage() * getDuration() - 2000)
                super.seekTo(msec);
            else {
                ToastUtils.toastMessage(getContext(), "网络连接错误，请检查网络后重试");
            }
        }
    }

    @Override
    public void start() {
        if (checkCanStart())
            super.start();
    }

    public boolean checkCanStart() {
        if (forbiddenStart) {
            ToastUtils.toastMessage(getContext(), "网络连接错误，请检查网络后重试");
            return false;
        }
        return true;
    }
}
